package com.itheima.web.controller;

import com.itheima.util.JwtUtil;
import com.itheima.web.manager.HouTaiUserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController()
@RequestMapping("/houtai")
@CrossOrigin("*")
public class HouTaiUserController {
    @Autowired
    private HouTaiUserManager houTaiUserManager;

    //权限校验
    @GetMapping("/verify")
    public String verify(@RequestHeader("Authorization")String token){
        try {
            //解析token
            Map map = JwtUtil.parseToken(token);
            System.out.println(map);
        }catch (Exception e){
            //解析失败提示错误，返回401
            e.printStackTrace();
            return "401";
        }
        //成功解析返回ok
        return "ok";
    }

    // 分页查询用户
    @GetMapping("/manage/users")
    public ResponseEntity findUserInfoByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            String cid,String keyWord) {
        // 调用manager
        return houTaiUserManager.findUserInfoByPage(pageNum, pageSize,cid,keyWord);
    }

    //获取用户地址
    @GetMapping("/user/location")
    public Map<String,String> findUserLocation(String userId){
        return houTaiUserManager.findUserLocation(userId);
    }
}
